package ch.fusun.baron.basic.client.ui;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import ch.fusun.baron.coatofarms.ui.CoatOfArmsData;
import ch.fusun.baron.coatofarms.ui.CoatOfArmsViewer;

/**
 * The activator
 */
public class Activator extends AbstractUIPlugin {

    private static final String ID = "ch.fusun.baron.basic.client.ui"; //$NON-NLS-1$
    private static BundleContext context;
    /**
     * Empty image to draw upon
     */
    public static final String ICONS_EMPTY = "icons/empty.png"; //$NON-NLS-1$

    /**
     * Pirate flag
     */
    private static final String ICONS_PIRATE = "icons/pirate.png";

    static BundleContext getContext() {
        return context;
    }

    private static Activator plugin;

    /**
     * Returns the shared instance
     * 
     * @return the shared instance
     */
    public static Activator getDefault() {
        return plugin;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext
     * )
     */
    @Override
    public void start(BundleContext bundleContext) throws Exception {
        Activator.context = bundleContext;
        plugin = this;
    }

    /**
     * @return the plugin
     */
    public static Activator getPlugin() {
        return plugin;
    }

    /**
     * @param path
     *            THe path to the image
     * @return The image descriptor
     */
    public static ImageDescriptor getImageDescriptor(String path) {
        return imageDescriptorFromPlugin(ID, path);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
     */
    @Override
    public void stop(BundleContext bundleContext) throws Exception {
        Activator.context = null;
    }

    /**
     * @param path
     *            The path to the image
     * @return The image
     */
    public static Image getImage(String path) {
        Image image = getDefault().getImageRegistry().get(path);
        if (image == null) {
            image = getImageDescriptor(path).createImage();
            getDefault().getImageRegistry().put(path, image);
        }
        return image;
    }

    /**
     * @param coa
     *            The coat of arms
     * @return The rendered image
     */
    public static Image getImageForCOA(CoatOfArmsData coa) {
        if (coa == null) {
            return getImage(ICONS_PIRATE);
        }
        Image image = getDefault().getImageRegistry().get(coa.uniqueCode());
        if (image == null) {
            Image empty = Activator.getImage(ICONS_EMPTY);
            image = new Image(empty.getDevice(), empty, SWT.IMAGE_COPY);
            CoatOfArmsViewer.drawOnGC(coa, image, 8, 8, 16, 16);
            getDefault().getImageRegistry().put(coa.uniqueCode(), image);
        }

        return image;
    }

}
